package com.mydubbo.util;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class IpUtil {
	
	public static String getLocalIp() {
		InetAddress addr;
		try {
			addr = InetAddress.getLocalHost();
			String ip=addr.getHostAddress().toString(); //获取本机ip
	        return ip;
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}  
		return null;
	}
	
	public static URL toURL(String address) {
		String[] ss = address.split(":");
		return new URL(ss[0], Integer.parseInt(ss[1]));
	}
	
	public static class URL {
		private String ip;
		private int port;
		public String getIp() {
			return ip;
		}
		public void setIp(String ip) {
			this.ip = ip;
		}
		public int getPort() {
			return port;
		}
		public void setPort(int port) {
			this.port = port;
		}
		public URL(String ip, int port) {
			super();
			this.ip = ip;
			this.port = port;
		}
		@Override
		public String toString() {
			return "URL [ip=" + ip + ", port=" + port + "]";
		}
	}

}
